package org.example.journal_items.Repository;

import org.example.journal_items.Entity.Diary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface DiaryRepository extends JpaRepository<Diary, Long> {

  Page<Diary> findByUserId(Long userId, Pageable pageable);

  long countByUserId(Long userId);

  long countByUserIdAndIsFavorite(Long userId, boolean isFavorite);

  @Query("SELECT DISTINCT CAST(d.createdAt AS date) FROM Diary d WHERE d.userId = :userId ORDER BY CAST(d.createdAt AS date) DESC")
  List<Date> findDistinctCreationDatesByUserId(@Param("userId") Long userId);

  // 获取用户的私密日记（按创建时间倒序）
  List<Diary> findByUserIdAndIsPrivateTrueOrderByCreatedAtDesc(Long userId);
}